import { View } from '@tarojs/components';
import Taro from '@tarojs/taro';
import React,{useMemo} from 'react';
import { getDispatch } from '../../../dva';
import './index.scss';

const StatusTxt = ({ className = '', order }) => {
  let statusText = '';

  switch (order.status) {
    case -1:
      statusText = '部分发货';
      break;
    case 1:
      statusText = '待支付';
      break;
    case 2:
      statusText = '已关闭';
      break;
    case 3:
      statusText = '已取消';
      break;
    case 4:
      statusText = '已完成';
      break;
    case 5:
      statusText = order.type == 4 ? '待接单' : '待发货';
      break;
    case 6:
      statusText = '待收货';
      break;
    case 7:
      statusText = '待发货';
      break;
    case 8:
      statusText = '待收货';
      break;
    case 9:
      statusText = '待自提';
      break;
    case 11:
      statusText = '待评价';
      break;
    case 12:
      statusText = '已退款';
      break;
    default:
      break;
  }

  return <View className={`${styles.StatusTxt} ${className} theme-color`}>{statusText}</View>;
};

const StatusAction = ({ className = '', order, onCancel, onRefresh }) => {
  const dispatch = useMemo(() => {
    return getDispatch();
  }, []);

  const confirmTakeGoods = (orderId) => {
    dispatch({
      type: 'order/confirmGetItemModel',
      payload: { orderId },
    }).then(() => {
      return onRefresh(orderId);
    });
  };
  const handleConfirmTakeGoods = (orderId) => {
    Taro.showModal({
      title: '操作提示',
      content: '请确保您已经收到货物，避免造成不必要的损失',
      cancelColor: '#000000',
      confirmColor: '#0091F2',
      success: function (res) {
        if (res.confirm) {
          confirmTakeGoods(orderId);
        } else if (res.cancel) {
        }
      },
    });
  };

  const handleGoComment = (id) => {
    // Taro.navigateTo({ url: `/orderSubPack/submitComment/index?id=${id}&appId=thjs_takeout_org` });
  };

  const handlePayNow = (orderInfo) => {
    Taro.navigateTo({url:`/pages/fineEngine/payment?id=${orderInfo.orderId}`})
  };

  const saveOrderDetail = (info) => {
    dispatch({
      type: 'order/saveOrderDetail',
      payload: { ...info },
    });
  };
  const showTransDetai = (orderInfo) => {
    let url = '/orderSubPack/logistics/index?orderId=' + orderInfo.orderId;
    if (orderInfo.logisticsList) {
      saveOrderDetail({
        id: orderInfo.orderId,
        logisticsList: [...orderInfo.logisticsList],
      });
      url = '/orderSubPack/logisticsList/index';
    }

    Taro.navigateTo({ url });
  };

  return (
    <View className={`StatusAction ${className}`}>
      <View className="actions">
        {order.showQXDDBtn?
        <View className="btn"
          onClick={() => onCancel(order.orderId)}
          key={order.orderId + '_orderBtn_' + order.status + '_cancle'}
        >
          <View className="txt">取消订单</View>
        </View>:null}
        {order.showLJFKBtn?
        <View className="btn btn-bg"
          onClick={() => handlePayNow(order)}
          key={order.orderId + '_orderBtn_' + order.status + '_pay'}
        >
          <View className="txt">立即支付</View>
        </View>:null}
        {order.showCKWLBtn?
        <View className="btn"
          onClick={() => showTransDetai(order)}
          key={order.orderId + '_orderBtn_' + order.status + '_translate'}
        >
          <View className="txt">查看物流</View>
        </View>:null}
        {order.showPJBtn?
        <View className="btn btn-bg"
          onClick={() => handleGoComment(order.orderId)}
          key={order.orderId + '_orderBtn_' + order.status + '_got'}
        >
          <View className="txt">去评价</View>
        </View>:null}
        {order.showQDZPBtn?
        <View className="btn btn-bg"
          key={order.orderId + '_orderBtn_' + order.status + '_dzp'}
          onClick={() => {
            let url = ''
            if(order.orderSource == 1){
              url = `/orderSubPack/orderDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            if(order.orderSource == 2){
              url = `/orderSubPack/cinemaDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            if(order.orderSource == 2){
              if(Taro.$channel?.includes('cinema')){
                url = `/mineSubPack/order-detail/index?id=${order.orderId}`
              }
              if(Taro.$channel?.includes('movie')){
                url = `/orderSubPack/cinemaDetail/index?orderId=${order.orderId}`;  /// 玩票详情
              }
            }
            if(order.orderSource == 3){
              url = `/orderSubPack/movieDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            if(order.orderSource == 4){
              url = `/orderSubPack/goodsDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            if(order.orderSource == 6){
              url = `/orderSubPack/powerDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            if(order.orderSource == 11){
              url = `/mineSubPack/food-detail/index?id=${order.orderId}`
            }
            if(order.orderSource == 5 || order.orderSource == 9 || order.orderSource == 8){
              url = `/orderSubPack/card-detail/index?orderId=${order.orderId}&type=${order.orderSource}`;
            }
            if(order.isOpenOrder){
              url = `/orderSubPack/activityDetail/index?orderId=${order.orderId}&t=${Date.now()}`;
            }
            Taro.navigateTo({
              url
            })
          }}
        >
          <View className="txt">取电子票</View>
        </View>:null}
        {order.showQRSHBtn?
        <View className="btn btn-bg"
          key={order.orderId + '_orderBtn_' + order.status + '_shou'}
          onClick={() => handleConfirmTakeGoods(order.orderId)}
        >
          <View className="txt">确认收货</View>
        </View>:null}
      </View>
    </View>
  );
};

export { StatusTxt, StatusAction };
